import React from 'react';
import connectFn from '@/utils/connectFn';
import Mixins from '@/components/Common/mixin';
import CreateCard from '@/components/CreateCard';
import CreateForm from '@/components/CreateForm';
import CreateTable from '@/components/CreateTable';
import { getFormData, getTableData } from './config';
import { StateType } from '@/common-typings';
import Observer from '@/utils/observer';
import constant from '@/const';

const { SUCCESS_CODE } = constant;

@connectFn(
  [{ global: ['validator'], level: ['levelState'] }],
  ['getLevelAdujustList', 'getLevelAdujustDownLoad'],
)
class LevelAdjustQuery extends Mixins {
  /**
   * 初始化tableFilters
   */
  initTableFilters = () => ({
    optType: [-1],
    creator: [],
  });

  public state = {
    submitLoading: false,
    exportLoading: false,
    disableCreator: false,
    list: [],
    pagination: this.defaultPagination,

    // 定义表单数据
    // tableFilters: this.initTableFilters(),
  };

  componentDidMount(): void {
    this.requestList();
  }

  /**
   * table-change时filter提前处理方法
   * @param filters
   */
  tableFiltersTool = (filters = {}) => {
    const initFilter = this.initTableFilters();
    const newFilter: StateType = {};
    Object.keys(filters).forEach(key => {
      if (filters[key].length) {
        if (key === 'optDesc') {
          newFilter.optType = filters[key];
        } else {
          newFilter[key] = filters[key];
        }
      }
    });

    return { ...initFilter, ...newFilter };
  };

  /**
   * 请求数据
   * @param params
   */
  public requestList = (params = {}, key?: string) => {
    const { getLevelAdujustList } = this.props;
    if (key === 'reset') {
      this.selectChange(0);
      // this.changeState({ tableFilters: this.initTableFilters() })
    }
    this.changePageLoading({ submitLoading: true });

    getLevelAdujustList(this.initRequestData(params, key)).then((res: StateType) => {
      const { code, data } = res;
      if (code === SUCCESS_CODE && data) {
        this.updateTableData(data);
      }
      this.changePageLoading({ submitLoading: false });
    });
  };

  /**
   * 导出excel
   */
  public exportExcel = (params = {}) => {
    const { getLevelAdujustDownLoad } = this.props;

    this.changePageLoading({ exportLoading: true });

    getLevelAdujustDownLoad(params).then((res: StateType) => {
      const { code } = res;
      if (code === SUCCESS_CODE) {
        Observer.trigger('get-export-download-list', {});
      }
      this.changePageLoading({ exportLoading: false });
    });
  };

  /**
   * 调整方式change事件回掉方法
   * @param value
   */
  public selectChange = (value: number) => {
    const { disableCreator } = this.state;
    // 系统调整时禁用调整人
    if (value === 1 && this.bindFormRef && !disableCreator) {
      this.bindFormRef.setFieldsValue({ creator: '' });
      this.changeState({ disableCreator: true });
    } else if (disableCreator) {
      this.changeState({ disableCreator: false });
    }
  };

  render() {
    return (
      <CreateCard>
        <CreateForm
          formConfig={getFormData(this)}
          ref={ref => {
            this.bindFormRef = ref;
          }}
        />
        <CreateTable
          tableConfig={getTableData(this)}
          ref={ref => {
            this.bindTableRef = ref;
          }}
        />
      </CreateCard>
    );
  }
}

export default LevelAdjustQuery;
